package com.neborosoft.FlyingDog.android.playing.modes;

import android.app.Activity;
import android.os.Parcelable;
import com.neborosoft.FlyingDog.android.MyActivity;
import com.neborosoft.android.events.HistoryManager;
import com.neborosoft.android.gui.Gui;
import com.neborosoft.android.gui.views.LayoutRadioButtonGroup;
import com.neborosoft.android.gui.wrappers.ListViewSwitcher;
import com.neborosoft.lib.Maps;
import com.neborosoft.lib.collections.tree.AsyncNTreeWrapper;
import com.neborosoft.lib.collections.tree.MultiStepsNavigationTree;
import com.neborosoft.lib.collections.tree.NTree;
import com.neborosoft.lib.handlers.OnHardwareBackPressed;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 16.02.13
 * Time: 2:31
 * To change this template use File | Settings | File Templates.
 */
public abstract class PlayingModeWithNavigationTabs extends TreeStructuredPlayingMode{
    private static final HistoryManager HISTORY_MANAGER = HistoryManager.getInstance();

    private int tabsLevel;
    private LayoutRadioButtonGroup tabs;
    private int enteredChildPosition = -1;
    private boolean onTabChangedShouldBeCalled = false;

    protected PlayingModeWithNavigationTabs(Activity activity, ListViewSwitcher listViewSwitcher,
                                            MultiStepsNavigationTree tree) {
        super(activity, listViewSwitcher, tree);

        int tabsResourceId = getTabsResourceId();
        tabsLevel = getTabsLevel();

        tabs = (LayoutRadioButtonGroup)getActivity().findViewById(tabsResourceId);
        if(tabs == null){
            throw new NullPointerException("could not find view with id = " + tabsResourceId);
        }

        setTabsVisibility(false);

        tabs.setOnSelectedChangedListener(new LayoutRadioButtonGroup.OnSelectedChanged() {
            @Override
            public void onSelectedChanged(boolean fromUser, LayoutRadioButtonGroup.LayoutRadioButton item,
                                          final LayoutRadioButtonGroup.LayoutRadioButton old)
            {
                if(onTabChangedShouldBeCalled){
                    int tabId = item.getId();
                    onTabChanged(tabId);
                }

                onTabChangedShouldBeCalled = true;

                if(!fromUser){
                    return;
                }

                HISTORY_MANAGER.pushHardwareBackPressedListener(new OnHardwareBackPressed() {
                    @Override
                    public void onBack() {
                        old.select();
                    }
                });
            }
        });
    }

    private MultiStepsNavigationTree getMultiStepsNavigationTree(){
        return (MultiStepsNavigationTree)getTree().getTree();
    }

    private void setStepsCount(int stepsCount){
        MultiStepsNavigationTree tree = getMultiStepsNavigationTree();
        tree.setStepsCount(stepsCount);
    }

    @Override
    protected void onChildExit() {
        if(tabsLevel == getCurrentLevel()){
            enteredChildPosition = -1;
        }
        setTabsVisibility(true);
    }

    private void onTabChanged(int newTabId){
        int level = getTabsLevel();
        AsyncNTreeWrapper tree = getTree();
        Object element = tree.getParent();

        boolean shouldRenterChild = enteredChildPosition != -1;
        if(shouldRenterChild){
            tree.exitToLevel(tabsLevel);
        }

        updateStepsCount(tabsLevel,newTabId);

        if(shouldRenterChild){
            enterChild(element, enteredChildPosition);
        }
    }

    private void setTabsVisibility(boolean visibility){
        Gui.setViewVisibility(tabs, visibility);
    }

    private void updateStepsCount(int level, int tabId){
        if(level == tabsLevel){
            int stepsCount = getStepsCountByTabId(tabId);
            setStepsCount(stepsCount);
            setTabsVisibility(true);
        } else {
            setStepsCount(MultiStepsNavigationTree.DEFAULT_STEPS_COUNT);
        }
    }

    @Override
    protected void onTreeItemClick(Object element, int position, int level) {
        int tabId = tabs.getSelectedItemId();
        updateStepsCount(level, tabId);
        if(level == tabsLevel){
            enteredChildPosition = position;
        }
        super.onTreeItemClick(element, position, level);
    }

    @Override
    public void onAnotherModeSelected() {
        super.onAnotherModeSelected();
        setTabsVisibility(false);
    }

    @Override
    public void showInListView(boolean restoreState) {
        setTabsVisibility(true);
        super.showInListView(restoreState);
    }

    protected abstract int getTabsResourceId();
    protected abstract int getTabsLevel();
    protected abstract int getStepsCountByTabId(int tabId);
}
